package com.googlecode.eclipse.navigatorext;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;

import com.googlecode.eclipse.navigatorext.l10n.Messages;
import com.googlecode.eclipse.navigatorext.preference.DateFormatPreferencePage;

/**
 * The activator class controls the plug-in life cycle
 */
public class Activator extends AbstractUIPlugin {

    // The plug-in ID
    public static final String PLUGIN_ID = "com.shera.eclipse.navigatorext";

    // The shared instance
    private static Activator plugin;

    /**
     * The constructor
     */
    public Activator() {
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)
     */
    public void start(BundleContext context) throws Exception {
        super.start(context);
        plugin = this;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
     */
    public void stop(BundleContext context) throws Exception {
        plugin = null;
        super.stop(context);
    }

    /**
     * Returns the shared instance
     * 
     * @return the shared instance
     */
    public static Activator getDefault() {
        return plugin;
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.eclipse.ui.plugin.AbstractUIPlugin#initializeDefaultPreferences(org.eclipse.jface.preference.IPreferenceStore
     * )
     */
    @Override
    protected void initializeDefaultPreferences(IPreferenceStore store) {
        DateFormatPreferencePage pref = new DateFormatPreferencePage();
        store = pref.getPreferenceStore();
    }

    public static String getId() {
        return PLUGIN_ID;
    }

    /**
     * Returns the workspace instance.
     */
    public static Shell getShell() {
        return getDefault().getWorkbench().getActiveWorkbenchWindow().getShell();
    }

    public static void errorDialog(String message, Throwable error) {
        Shell shell = getShell();
        if (message == null) {
            message = Messages.Dialog_msg_err;
        }
        message = message + " " + error.getMessage();

        getDefault().getLog().log(new Status(IStatus.ERROR, getId(), IStatus.OK, message, error));

        MessageDialog.openError(shell, Messages.Dialog_title_err, message);
    }

    /**
     * @param error
     */
    public static void logError(String message, Throwable error) {
        if (message == null) {
            message = error.getMessage();
            if (message == null) {
                message = error.toString();
            }
        }
        getDefault().getLog().log(new Status(IStatus.ERROR, getId(), IStatus.OK, message, error));
    }

    public static void logInfo(String message) {
        getDefault().getLog().log(new Status(IStatus.INFO, getId(), IStatus.OK, message, null));
    }

    public static void errorDialog(String message) {
        Shell shell = getShell();
        MessageDialog.openError(shell, Messages.Dialog_title_err, message);
    }
}
